package com.sjtu.rental.validation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class CheckLoginInterceptor extends HandlerInterceptorAdapter {
	
	private List<String> needNotValidate;

	public List<String> getNeedNotValidate() {
		return needNotValidate;
	}

	public void setNeedNotValidate(List<String> needNotValidate) {
		this.needNotValidate = needNotValidate;
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String url = request.getRequestURI().toString();
		url = url.replaceFirst(request.getContextPath(), "");
		if (needNotValidate.contains(url)) {
			return true;
		} else {
			boolean check = request.getSession().getAttribute("user") != null;
			if (check) {
				return true;
			} else {
				request.getRequestDispatcher("/login?error=nolog").forward(request, response);
				return false;
			}
			
		}
		
	}

}
